package cc.ding.servlet;

import cc.ding.entity.Employee;
import cc.ding.service.Impl.StatisticsServiceImpl;
import cc.ding.service.StatisticsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/statistics.action")
public class StatisticsServlet extends HttpServlet {
    StatisticsService statisticsService = new StatisticsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = req.getParameter("opr");
        switch (opr){
            case "yg":
                //员工查看统计
                HttpSession session = req.getSession();

                Employee employee = (Employee) session.getAttribute("employee");

                List<Map<String, String>> list = statisticsService.empStatistics(employee.getId());

                String jsonString = JSON.toJSONString(list, SerializerFeature.WriteNullStringAsEmpty);

                resp.getWriter().print(jsonString);
                return;
            case "admin":
                //管理员查看的统计
                return;
        }
    }
}
